#include "Circle3DHandler.h"
#include "CmdManager.h"
#include "SendPointHandlerBase.h"
#include "Scene.h"
#include "Ucs.h"
#include "qundostack.h"
#include "Operation/OpAdd.h"
#include "geometricutility.h"
#include "DblPoint.h"
#include "QtOSGWidget.h"
#include "SelectHandler3D.h"
#include "PointHandler.h"
#include "QtOSGWidget.h"

Circle3DHandler::Circle3DHandler()
{
}

Circle3DHandler::~Circle3DHandler()
{
}

void Circle3DHandler::input(const QVariant& data, const QString& rawInput, CommandSource cs)
{
	auto sph = getSendPointHandler();
	auto scene = getScene();
	auto widget3D = getWidget();
	if (data.type() == QVariant::Bool/* && !data.toBool()*/)
	{
		if (m_cir)
		{
			widget3D->pauseRenderThread();
			scene->removeChild(m_cir);
			widget3D->resumeRenderThread();
		}
		emit finished();
		return;
	}

	if (status() == 0)
	{
		if (sph)
		{
			sph->setPointMask(CS_LBTNDOWN);
		}
		m_pointStateMachine = std::make_shared<PointHandler>();
		m_pointStateMachine->setCmdManager(cmdManager());
		m_cir = std::make_shared<Arc3D>(scene);
		m_cir->setColor(getCurrentColor());
		widget3D->pauseRenderThread();
		scene->addChild(m_cir);
		widget3D->resumeRenderThread();
		m_pointStateMachine->setPrompt0(tr("指定圆的起点"));
		m_pointStateMachine->input(data, rawInput, cs);
		setStatus(1);
		return;
	}

	if (status() == 1)
	{
		m_pointStateMachine->input(data, rawInput, cs);
		if (m_pointStateMachine->done())
		{
			m_startPt = m_pointStateMachine->getPoint();
			if (sph)
			{
				sph->setPointMask(CS_LBTNDOWN | CS_MOUSEMOVE);
			}
			show3DRubberLine(true);
			set3DRubberLineP1(m_startPt);
			set3DRubberLineP2(m_startPt);
			QStringList optionPrompts, optionInputs;
			optionPrompts << "中心" << "终点";
			optionInputs << "C" << "E";
			m_pointStateMachine->setPrompt0(tr("指定圆的第二点或"), optionPrompts, optionInputs);
			m_pointStateMachine->reset();
			setStatus(2);
		}
		return;
	}

	if (status() == 2)
	{
		if (data.type() == QVariant::Vector3D && cs == CS_MOUSEMOVE)
		{
			auto pt = data.value<QVector3D>();
			set3DRubberLineP2({ pt.x(), pt.y(), pt.z() });
		}
		m_pointStateMachine->input(data, rawInput, cs);
		if (m_pointStateMachine->done())
		{
			show3DRubberLine(false);
			if (m_pointStateMachine->isResultPoint())
			{
				m_secondPt = m_pointStateMachine->getPoint();
				m_pointStateMachine->setPrompt0(tr("指定圆的终点"));
				m_pointStateMachine->reset();
				setStatus(3);
			}
			else
			{
				const QString resultInput = m_pointStateMachine->getResultInput();
				if (resultInput == QString("C"))
				{
					m_pointStateMachine->setPrompt0(tr("指定圆的中心"));
					m_pointStateMachine->reset();
					setStatus(4);
				}
				else if (resultInput == QString("E"))
				{
					m_pointStateMachine->setPrompt0(tr("指定圆的终点"));
					m_pointStateMachine->reset();
					setStatus(6);
				}
			}
		}
		return;
	}
	//start - second - end
	if (status() == 3)
	{
		if (cs == CS_MOUSEMOVE)
		{
			if (data.type() == QVariant::Vector3D)
			{
				auto pt = data.value<QVector3D>();
				m_endPt = { pt.x(), pt.y(), pt.z() };
				m_cir->setCircle3P(m_startPt, m_secondPt, m_endPt);
			}
		}
		else
		{
			m_pointStateMachine->input(data, rawInput, cs);
			if (m_pointStateMachine->done())
			{
				m_endPt = m_pointStateMachine->getPoint();
				if (!m_cir->setCircle3P(m_startPt, m_secondPt, m_endPt))
					return;
				drawFinished(scene);
				emit finished();
			}
		}
		return;
	}
	//start - center
	if (status() == 4)
	{
		if (data.type() == QVariant::Vector3D && cs == CS_MOUSEMOVE)
		{
			auto pt = data.value<QVector3D>();
			set3DRubberLineP2({ pt.x(), pt.y(), pt.z() });
		}
		m_pointStateMachine->input(data, rawInput, cs);
		if (m_pointStateMachine->done())
		{
			show3DRubberLine(false);
			m_secondPt = m_pointStateMachine->getPoint();
			m_pointStateMachine->setPrompt0(tr("指定圆的终点"));
			m_pointStateMachine->reset();
			setStatus(5);
		}
		return;
	}
	//start - center - end
	if (status() == 5)
	{
		if (cs == CS_MOUSEMOVE)
		{
			if (data.type() == QVariant::Vector3D)
			{
				auto pt = data.value<QVector3D>();
				m_endPt = { pt.x(), pt.y(), pt.z() };
				m_cir->setCircleCSE(m_secondPt, m_startPt, m_endPt);
			}
		}
		else
		{
			m_pointStateMachine->input(data, rawInput, cs);
			if (m_pointStateMachine->done())
			{
				m_endPt = m_pointStateMachine->getPoint();
				if (!m_cir->setCircleCSE(m_secondPt, m_startPt, m_endPt))
					return;
				drawFinished(scene);
				emit finished();
			}
		}
		return;
	}
	//start - end
	if (status() == 6)
	{
		if (data.type() == QVariant::Vector3D && cs == CS_MOUSEMOVE)
		{
			auto pt = data.value<QVector3D>();
			set3DRubberLineP2({ pt.x(), pt.y(), pt.z() });
		}
		m_pointStateMachine->input(data, rawInput, cs);
		if (m_pointStateMachine->done())
		{
			show3DRubberLine(false);
			m_secondPt = m_pointStateMachine->getPoint();
			m_pointStateMachine->setPrompt0(tr("指定圆的中心"));
			m_pointStateMachine->reset();
			setStatus(7);
		}
		return;
	}
	//start - end - center
	if (status() == 7)
	{
		if (cs == CS_MOUSEMOVE)
		{
			if (data.type() == QVariant::Vector3D)
			{
				auto pt = data.value<QVector3D>();
				m_endPt = { pt.x(), pt.y(), pt.z() };
				m_cir->setCircleCSE(m_endPt, m_startPt, m_secondPt);
			}
		}
		else
		{
			m_pointStateMachine->input(data, rawInput, cs);
			if (m_pointStateMachine->done())
			{
				m_endPt = m_pointStateMachine->getPoint();
				if (!m_cir->setCircleCSE(m_endPt, m_startPt, m_secondPt))
					return;
				drawFinished(scene);
				emit finished();
			}  
		}
		return;
	}
}

void Circle3DHandler::drawFinished(Scene * scene)
{
	auto undoStack = scene->undoStack();
	std::set<std::shared_ptr<INode>> children;
	children.insert(m_cir);
	undoStack->push(new OpAdd(children));
}



